namespace _388 {

var lengthLongestPath = function(input: string): number {
    var answer = 0
    var currentPos = 0
    function loop(level: number) {
        var isFile = false
        var lastPathLength = 0
        var longestFilePath = 0
        for(var i = currentPos; i < input.length; i++) {
            if (input[i] === '.' && i < input.length - 1) {
                isFile = true
            }
            else if (input[i] === '\n') {
                lastPathLength = i - currentPos
                if (isFile && i - currentPos > longestFilePath) {
                    longestFilePath = lastPathLength 
                    isFile = false
                }
                currentPos = i + 1
            }
            else if(input[i] === '\t') {
                var jumpToLevel = 1
                currentPos += level + 1
                let tailLength = loop(level + 1) 
                if ( tailLength > 0) {
                    longestFilePath = lastPathLength + tailLength  
                }
            }
        }
        if (isFile && i - currentPos > longestFilePath) {
            longestFilePath = i - currentPos
            isFile = false
        }
        return longestFilePath 
    } 
    answer = loop(0)
    return answer
};    

console.assert( lengthLongestPath( "dir\n\tsubdir1\n\t\tfile1.ext\n\t\tsubsubdir1\n\tsubdir2\n\t\tsubsubdir2\n\t\t\tfile2.ext") === 32)
console.assert( lengthLongestPath( "test.txt\nfile1.txt") === 9)

}